package com.vector.demo.controller.admin;

import com.vector.demo.pojo.Student;
import com.vector.demo.pojo.Teacher;
import com.vector.demo.service.StudentService;
import com.vector.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.Struct;
import java.util.List;

/**
 * admin 人员管理
 */
@Controller
@RequestMapping("/admin")
public class StaffManagementController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    /**
     * 学生管理
     * @return
     */
    @RequestMapping("/studentStaffManagementController")
    public String studentStaffManagementController(Model model) {
        List<Student> list = studentService.selectAllStudent();
        model.addAttribute("studentList",list);
        return "admin/StaffManagement/studentStaffManagement";
    }
    /**
     * 预更新 学生
     */
    @RequestMapping("/preModifyStudentStaffManagementController")
    public String modifyStudentStaffManagementController(Model model,int id) {
        Student student = studentService.selectStudentById(id);
        model.addAttribute("student",student);
        return "admin/StaffManagement/modifyStudentStaff";
    }

    /**
     * 更新
     */
    @RequestMapping("/modifyStudentStaffManagementController")
    public String modifyStudentStaffManagementController(Model model, String username,String pwd,String newpwd,String repwd,String classname, int id) {
        Student student = new Student();
        //判断非空
        if(username!=null&& "".equals(username)&&pwd!=null&& "".equals(pwd)&&newpwd!=null&& "".equals(newpwd)&&repwd!=null&& "".equals(repwd)&&classname!=null&& "".equals(classname)) {
            if(repwd.equals(newpwd)) {
                student.setS_id(id);
                student.setS_name(username);
                student.setS_pwd(newpwd);
                student.setCla_name(classname);
                studentService.modifyStudent(student);
                model.addAttribute("msg","操作成功！");
                return "admin_index";
            }else{
                model.addAttribute("msg","两次密码不一致！");
            }
        }else {
            model.addAttribute("msg","信息不能为空！");
        }
        return "error/operationException";
    }
    /**
     *添加 学生
     */
    @RequestMapping("addStudentStaffManagementController")
    public String addStudentStaffManagementController(Model model, String username,String pwd,String repwd,String classname, int id) {
        Student student = new Student();
        //判断非空
        if(username!=null&& "".equals(username)&&pwd!=null&& "".equals(pwd) && repwd!=null&& "".equals(repwd)&&classname!=null&& "".equals(classname)) {
            if(repwd.equals(pwd)) {
                student.setS_id(id);
                student.setS_name(username);
                student.setS_pwd(pwd);
                student.setCla_name(classname);
                studentService.modifyStudent(student);
                model.addAttribute("msg","操作成功！");
                return "admin_index";
            }else{
                model.addAttribute("msg","两次密码不一致！");
            }
        }else {
            model.addAttribute("msg","信息不能为空！");
        }
        return "error/operationException";
    }


    /**
     * 删除 学生
     */
    @RequestMapping("/dropStudentStaffManagementController")
    public String dropStudentStaffManagementController(int id,Model model) {
        if(studentService.dropStudentById(id) == null){ //1
            return "admin_index";
        }else {
            model.addAttribute("msg","删除失败");
            return "error/operationException";
        }
    }


    /*********************************************************教室管理****************************************************************/


    /**
     * 教师管理
     */
    @RequestMapping("/teacherStaffManagementController")
    public String teacherStaffManagementController(Model model) {
        List<Teacher> list = teacherService.queryAllTeacher();
        model.addAttribute("teacherList",list);
        return "admin/StaffManagement/teacherStaffManagement";
    }

    /**
     * 教师 预更新
     */
    @GetMapping("/preModifyTeacherStaffManagementController")
    public String preModifyTeacherStaffManagementController(Model model, int id) {
        Teacher teacher = teacherService.queryTeacherById(id);
        model.addAttribute("teacher",teacher);
        return "admin/StaffManagement/modifyTeacherStaff";
    }

    /**
     * 教师 更新
     */
    @RequestMapping("/modifyTeacherStaffManagementController")
    public String modifyTeacherStaffManagementController(String username, String pwd, String newpwd, String repwd, String type, Model model, int id) {
        Teacher teacher = new Teacher();
        if(username!=null && !"".equals(username) && pwd!=null&& !"".equals(pwd)&&newpwd!=null&& !"".equals(newpwd)&&repwd!=null && !"".equals(repwd) && type!=null && !"".equals(type)) {
            if(repwd.equals(newpwd)) {
                teacher.setT_id(id);
                teacher.setT_name(username);
                teacher.setT_pwd(newpwd);
                teacher.setT_type(type);
                teacherService.updateTeacher(teacher);
                model.addAttribute("msg","操作成功！");
                return "/admin_index";
            }else{
                model.addAttribute("msg","两次密码不一致！");
                return "error/operationException";
            }
        }else {
            model.addAttribute("msg","信息不能为空！");
            return "error/operationException";
        }
    }

    /**
     *教师 删除
     */
    @RequestMapping("/dropTeacherStaffManagementController")
    public String dropTeacherStaffManagementController(int id, Model model) {
        if(teacherService.deleteTeacher(id) == 1) {
            return "admin_index";
        } else {
            model.addAttribute("msg","删除教师失败！");
            return "error/operationException";
        }
    }

    /**
     * 教师 添加
     */
    @RequestMapping("addTeacherStaffManagementController")
    public String addTeacherStaffManagementController(Model model, String username,String pwd,String repwd,String type, int id) {
        Teacher teacher = new Teacher();
        if (username != null && !"".equals(username) && pwd != null && !"".equals(pwd)  && repwd != null && !"".equals(repwd) && type != null && !"".equals(type)) {
            if (repwd.equals(pwd)) {
                teacher.setT_id(id);
                teacher.setT_name(username);
                teacher.setT_pwd(pwd);
                teacher.setT_type(type);
                teacherService.updateTeacher(teacher);
                model.addAttribute("msg", "操作成功！");
                return "/admin_index";
            } else {
                model.addAttribute("msg", "两次密码不一致！");
                return "error/operationException";
            }
        } else {
            model.addAttribute("msg", "信息不能为空！");
            return "error/operationException";
        }
    }
}
